//
//  MePageViewController.m
//  SuperNews
//
//  Created by qiang on 16/1/9.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "MePageViewController.h"
#import "CollectViewController.h"
#import<MessageUI/MFMailComposeViewController.h>

@interface MePageViewController () <UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate>
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) UIButton *btn;
@end

@implementation MePageViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableV reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubviews];
    
}
- (void)createSubviews
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    self.imageV.backgroundColor = [UIColor colorWithRed:0.413 green:0.000 blue:0.004 alpha:1.000];
    [self.view addSubview:self.imageV];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(SCREEN_WIDTH / 2 - 50, 100, 100, 100);
    [self.btn setImage:[UIImage imageNamed:@"comment_profile_default"] forState:UIControlStateNormal];
    [self.imageV addSubview:self.btn];
    self.tableV.tableHeaderView = headView;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    CGRect frame = self.imageV.frame;
    //向上滑动
    if (offsetY > 0) {
        frame.origin.y = - offsetY;
        self.imageV.frame = frame;
    }else {
        //向下滑动
        //获取拉伸后图片的高度
        CGFloat height = 250 + ABS(offsetY);
        //修改imageView的frame
        self.btn.frame = CGRectMake(SCREEN_WIDTH / 2 - 100, 100 + ABS(offsetY), 200, 200);
        self.imageV.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    if (0 == indexPath.section) {
        cell.textLabel.text = @"我的收藏";
        cell.imageView.image = [UIImage imageNamed:@"share_platform_yixintimeline"];
    }
    if (1 == indexPath.section) {
        cell.textLabel.text = @"清除缓存";
        cell.imageView.image = [UIImage imageNamed:@"share_platform_instapaper"];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fMB", [self folderSizeAtPath:path]];
    }
    if (2 == indexPath.section) {
        cell.textLabel.text = @"联系我们";
        cell.imageView.image = [UIImage imageNamed:@"achi_calendar"];
    }
    
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        CollectViewController *collectVC = [[CollectViewController alloc] init];
        UINavigationController *collectNC = [[UINavigationController alloc] initWithRootViewController:collectVC];
        [collectNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_navigation_background"] forBarMetrics:UIBarMetricsDefault];
        [self presentViewController:collectNC animated:YES completion:^{
            
            
        }];
    }
    if (1 == indexPath.section) {
        // 清除缓存
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"总的缓存大小为%.2fMB, 你确定要删除吗?", [self folderSizeAtPath:path]] preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self clearCache:path];
            [self.tableV reloadData];
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
            
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:^{
            }];
            
        }]];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (3 == indexPath.section) {
        return 300;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 4;
}

//单个文件的大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
    
}
//遍历文件夹获得文件夹大小，返回多少M
- (float) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath] ;
    }
    return folderSize/(1024.0*1024.0) + [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
}

// 清除缓存实现方法
- (void)clearCache:(NSString *)path {
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    // 清除保存的图片
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
